package com.example.studentregistration.controller;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.example.studentregistration.pojo.Major;
import com.example.studentregistration.pojo.Users;
import com.example.studentregistration.service.MajorService;
import com.example.studentregistration.util.Result;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@RestController
@RequestMapping("/api")
public class MajorController {
    
    @Autowired
    private MajorService majorService;
    
    // 添加专业
    @PostMapping("/major/add")
    public Result<?> addMajor(@RequestBody Major major) {
        return majorService.insertMajor(major);
    }
    
    // 修改专业
    @PutMapping("/major/update")
    public Result<?> updateMajor(@RequestBody Major major) {
        return majorService.updateMajor(major);
    }
    
    // 删除专业
    @DeleteMapping("/major/delete/{id}")
    public Result<?> deleteMajor(@PathVariable Integer id) {
        return majorService.deleteMajor(id);
    }
    
    // 批量删除专业
    @PostMapping("/major/batchDelete")
    public Result<?> batchDeleteMajor(@RequestBody List<Integer> ids) {
        if (ids == null || ids.isEmpty()) {
            return Result.fail("请选择要删除的专业");
        }
        int result = majorService.batchDeleteMajor(ids);
        if (result > 0) {
            return Result.success("删除成功" + result + "条记录");
        }
        return Result.fail("删除失败");
    }
    
    // 分页查询所有专业
//    @GetMapping("/major/queryAll")
    @GetMapping("/major")
    public Result<Page> queryAllMajors(@RequestParam(defaultValue = "1") Integer currentPage,
                                      @RequestParam(defaultValue = "10") Integer pageSize) {
        Page page = majorService.queryAllMajors(currentPage, pageSize);
        return Result.success(page);
    }

    // 根据ID查询专业
    @GetMapping("/major/queryById/{id}")
    public Result<Major> queryMajorById(@PathVariable Integer id) {
        return majorService.queryMajorById(id);
    }
    
    // 根据专业名称查询专业
    @GetMapping("/major/queryByName")
    public Result<Major> queryMajorByName(@RequestParam String majorName) {
        return majorService.queryMajorByName(majorName);
    }
    
    // 根据专业代码查询专业
    @GetMapping("/major/queryByCode")
    public Result<Major> queryMajorByCode(@RequestParam String majorCode) {
        return majorService.queryMajorByCode(majorCode);
    }
    
    // 导出专业信息
    @GetMapping("/major/export")
    public void exportMajorExcel(HttpServletResponse response) throws IOException {
        majorService.exportExcel(response);
    }
    //文件导入
    @PostMapping("/major/import")
    public Result<?> importMajorExcel( MultipartFile file) throws IOException {
        //获取文件流输入对象
        InputStream is = file.getInputStream();
        //读取流
        ExcelReader reader = ExcelUtil.getReader(is);
//        List<Users> list = reader.readAll(Users.class);
        List<Major> list = reader.readAll(Major.class);
        //批处理
        try {
//            list.stream().forEach(user ->
//                    userService.insertUser(user));
            list.stream().forEach(major ->
                    majorService.insertMajor(major));
        } catch (Exception e) {
            Result.fail("导入失败");
        }
        return Result.success("导入成功");
    }
}